package com.linkoog.devtools.code_generator.wizard.generate.database.table.custom;

import com.linkoog.devtools.code_generator.database.domain.ColumnInfo;
import com.intellij.ui.table.JBTable;
import com.linkoog.devtools.intellij.ui.table.cell_editor.JCheckBoxTableCellEditor;
import com.linkoog.devtools.intellij.ui.table.cell_reander.JCheckBoxTableCellRenderer;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;


public class ColumnInfoJTableModel extends AbstractTableModel implements Serializable {

    private static final Logger logger = LoggerFactory.getLogger(DbtableInfoJTableModel.class);

    private final List<String> columnList = Arrays.asList("序号","列名","类型","Java字段","Java类型","注释","查询","新增","更新","字典","选中");

    private final List<ColumnInfo> dataList = new ArrayList<>();


    public ColumnInfoJTableModel(){
    }

    public ColumnInfoJTableModel(Collection<ColumnInfo> newRowList) {
        addRow(newRowList);
    }

    public void initTableColumn(JBTable jbTable){
        DefaultTableCellRenderer tableCellRenderer = new DefaultTableCellRenderer();
        tableCellRenderer.setHorizontalAlignment(SwingConstants.CENTER);


        TableColumn column0 = jbTable.getColumn("序号");
        column0.setMinWidth(40);
        column0.setMaxWidth(40);
        column0.setPreferredWidth(40);
        column0.setCellRenderer(tableCellRenderer);



        TableColumn column1 = jbTable.getColumn("列名");
        column1.setMinWidth(120);
        column1.setMaxWidth(120);
        column1.setPreferredWidth(120);
        //column1.setCellRenderer(tableCellRenderer);


        JCheckBoxTableCellRenderer column6Renderer = new JCheckBoxTableCellRenderer();
        column6Renderer.setToolTipText("查询时，是否为条件字段");

        TableColumn column6 = jbTable.getColumn("查询");
        column6.setCellEditor(new JCheckBoxTableCellEditor());
        column6.setCellRenderer(column6Renderer);
        column6.setMinWidth(46);
        column6.setMaxWidth(46);
        column6.setPreferredWidth(46);


        JCheckBoxTableCellRenderer column7Renderer = new JCheckBoxTableCellRenderer();
        column7Renderer.setToolTipText("新增时，是否为必填的字段");

        TableColumn column7 = jbTable.getColumn("新增");
        column7.setCellEditor(new JCheckBoxTableCellEditor());
        column7.setCellRenderer(column7Renderer);
        column7.setMinWidth(46);
        column7.setMaxWidth(46);
        column7.setPreferredWidth(46);


        JCheckBoxTableCellRenderer column8Renderer = new JCheckBoxTableCellRenderer();
        column8Renderer.setToolTipText("更新时，是否为必填的字段");

        TableColumn column8 = jbTable.getColumn("更新");
        column8.setCellEditor(new JCheckBoxTableCellEditor());
        column8.setCellRenderer(column8Renderer);
        column8.setMinWidth(46);
        column8.setMaxWidth(46);
        column8.setPreferredWidth(46);


        JCheckBoxTableCellRenderer column9Renderer = new JCheckBoxTableCellRenderer();
        column9Renderer.setToolTipText("是否为字典字段");

        TableColumn column9 = jbTable.getColumn("字典");
        column9.setCellEditor(new JCheckBoxTableCellEditor());
        column9.setCellRenderer(column9Renderer);
        column9.setMinWidth(46);
        column9.setMaxWidth(46);
        column9.setPreferredWidth(46);


        JCheckBoxTableCellRenderer column10Renderer = new JCheckBoxTableCellRenderer();
        column10Renderer.setToolTipText("是否为实体类的字段");

        TableColumn column10 = jbTable.getColumn("选中");
        column10.setCellEditor(new JCheckBoxTableCellEditor());
        column10.setCellRenderer(column10Renderer);
        column10.setMinWidth(46);
        column10.setMaxWidth(46);
        column10.setPreferredWidth(46);

    }

    @Override
    public int getRowCount() {
        return dataList.size();
    }

    @Override
    public int getColumnCount() {
        return columnList.size();
    }

    @Override
    public String getColumnName(int columnIndex) {
        /*if (columnIndex == 4){
            return "";
        }*/
        return columnList.get(columnIndex);
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        ColumnInfo columnInfo = dataList.get(rowIndex);
        switch (columnIndex){
            case 0: return rowIndex;
            case 1: return columnInfo.getColumnName();
            case 2: return columnInfo.getColumnType();
            case 3: return columnInfo.getJavaFieldName();
            case 4: return columnInfo.getJavaFieldType();
            case 5: return columnInfo.getColumnComment();
            case 6: return columnInfo.getQueryChecked();
            case 7: return columnInfo.getInsertChecked();
            case 8: return columnInfo.getUpdateChecked();
            case 9: return columnInfo.getDictChecked();
            case 10: return columnInfo.getChecked();
            default: return "";
        }
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        switch (columnIndex){
            case 3:
            case 4:
            case 5:
            case 6:
                return true;
            case 7:
                return true;
            case 8:
                return true;
            case 9:
                return true;
            case 10:
                return true;
        }
        return false;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        ColumnInfo columnInfo = dataList.get(rowIndex);
        switch (columnIndex){
            case 3:
                columnInfo.setJavaFieldName((String) aValue);
                break;
            case 4:
                columnInfo.setJavaFieldType((String) aValue);
                break;
            case 5:
                columnInfo.setColumnComment((String) aValue);
                break;
            case 6:
                columnInfo.setQueryChecked((Boolean) aValue);
                break;
            case 7:
                columnInfo.setInsertChecked((Boolean) aValue);
                break;
            case 8:
                columnInfo.setUpdateChecked((Boolean) aValue);
                break;
            case 9:
                columnInfo.setDictChecked((Boolean) aValue);
                break;
            case 10:
                columnInfo.setChecked((Boolean) aValue);
                break;
        }
    }

    public final boolean hasElements() {
        return dataList.size() > 0;
    }


    public final void addRow(@NotNull ColumnInfo newRow) {
        dataList.add(newRow);
        super.fireTableDataChanged();
    }

    public final void addRow(@NotNull Collection<ColumnInfo> newRowList) {
        if (newRowList != null && !newRowList.isEmpty()){
            dataList.addAll(newRowList);
        }
        super.fireTableDataChanged();
    }

    public final void removeRow(int rowIndex) {
        dataList.remove(rowIndex);
        super.fireTableDataChanged();
    }

    public final void clear() {
        dataList.clear();
        super.fireTableDataChanged();
    }

    public final void reset(Collection<ColumnInfo> newRowList) {
        dataList.clear();
        if (newRowList != null && !newRowList.isEmpty()){
            dataList.addAll(newRowList);
        }
        super.fireTableDataChanged();
    }


    public final ColumnInfo getRowData(int rowIndex) {
        return dataList.get(rowIndex);
    }

    public List<String> getColumnList() {
        return columnList;
    }



}